/**  
* Copyright (c) 2009, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// created by noellynch
// Jun 26, 2009

package com.sceneworker.editors.glextchecker;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.concurrent.Callable;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import org.lwjgl.opengl.ARBFragmentShader;
import org.lwjgl.opengl.ARBShadingLanguage100;
import org.lwjgl.opengl.ARBVertexShader;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;

import com.jme.scene.state.TextureState;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.geom.BufferUtils;
import com.myjavautils.swing.MigLayoutUtils;
import com.sceneworker.app.globals.SceneWorkerPreferencesPanel.IPrefsPanel;
import static com.myjavautils.swing.SwingComponentFactory.*;

public class GLExtCheckerPanel extends JPanel implements IPrefsPanel{

	private		JList				mcl_extList;
	private		ArrayList<String>	mstra_extensions;
	private		JTextField			mcl_input;
	
	public	GLExtCheckerPanel() {
		setLayout(new MigLayout("inset 5"));
		
		JLabel			lcl_lbl = makeALabel("Extensions : Search");
		mcl_input = makeATextField();
		mcl_input.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent e) {
				filter();
			}

			public void keyReleased(KeyEvent e) {
				filter();
			}

			public void keyTyped(KeyEvent e) {
			}
			
		});
		
		add(lcl_lbl, "split 2");
		add(mcl_input, "wrap, grow");
		
		initialiseList();
	}
	
	private	void	initialiseList() {
		mcl_extList = makeAList();
		
		JScrollPane		lcl_scroller = new JScrollPane();
		lcl_scroller.setViewportView(mcl_extList);
		lcl_scroller.setMinimumSize(new Dimension(250, 250));
		add(lcl_scroller, "width 50%");
		
		getExtensions();
	}
	
	private	void		getExtensions() {
		final	JPanel		lcl_thisPanel = this;
		
		GameTaskQueueManager.getManager().update(new Callable<Void>() {
			public Void call() throws Exception {
				mstra_extensions = new ArrayList<String>();
				DefaultListModel		lcl_model = new DefaultListModel();
				
				String		lstr_temp = GL11.glGetString(GL11.GL_EXTENSIONS);
				String		lstra_parts[] = lstr_temp.split(" ");
				
				for(String lstr_part : lstra_parts) {
					lcl_model.addElement(lstr_part);
					mstra_extensions.add(lstr_part);
				}
				
				mcl_extList.setModel(lcl_model);
				
				//------------------------------------------------------
				boolean		lb_supported = GLContext.getCapabilities().GL_ARB_shader_objects
                												&& GLContext.getCapabilities().GL_ARB_fragment_shader
                												&& GLContext.getCapabilities().GL_ARB_vertex_shader
                												&& GLContext.getCapabilities().GL_ARB_shading_language_100;
				
				JPanel		lcl_panel = MigLayoutUtils.makeStandardPanel();
				
				JLabel		lcl_supportedLabel = makeALabel("");
				lcl_panel.add(lcl_supportedLabel, "wrap");
				if(!lb_supported) {
					lcl_supportedLabel.setText("Shaders are not supported by your card.");
					lcl_thisPanel.add(lcl_panel, "wrap");
					return null;
				}
				
				IntBuffer mcl_buf = BufferUtils.createIntBuffer(16);
				
				GL11.glGetInteger(ARBVertexShader.GL_MAX_VERTEX_ATTRIBS_ARB, mcl_buf);
				JLabel		lcl_maxVertexAttrs = makeALabel("GL_MAX_VERTEX_ATTRIBS: " + mcl_buf.get(0));
				lcl_panel.add(lcl_maxVertexAttrs, "wrap");

				GL11.glGetInteger(ARBVertexShader.GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB,	mcl_buf);
				JLabel		lcl_maxVertexUniforms = makeALabel("GL_MAX_VERTEX_UNIFORM_COMPONENTS: " + mcl_buf.get(0));
				lcl_panel.add(lcl_maxVertexUniforms, "wrap");

				GL11.glGetInteger(ARBFragmentShader.GL_MAX_FRAGMENT_UNIFORM_COMPONENTS_ARB,	mcl_buf);
				JLabel		lcl_maxFragmentUniforms = makeALabel("GL_MAX_FRAGMENT_UNIFORM_COMPONENTS: " + mcl_buf.get(0));
				lcl_panel.add(lcl_maxFragmentUniforms, "wrap");
				
				GL11.glGetInteger(ARBFragmentShader.GL_MAX_TEXTURE_COORDS_ARB, mcl_buf);
				JLabel		lcl_maxTexCoords = makeALabel("GL_MAX_TEXTURE_COORDS: " + mcl_buf.get(0));
				lcl_panel.add(lcl_maxTexCoords, "wrap");
				
				GL11.glGetInteger(ARBFragmentShader.GL_MAX_TEXTURE_IMAGE_UNITS_ARB, mcl_buf);
				JLabel		lcl_maxTexUnits = makeALabel("GL_MAX_TEXTURE_IMAGE_UNITS: " + mcl_buf.get(0));
				lcl_panel.add(lcl_maxTexUnits, "wrap");

				GL11.glGetInteger(ARBVertexShader.GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB, mcl_buf);
				JLabel		lcl_maxImageTexUnits = makeALabel("GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: " + mcl_buf.get(0));
				lcl_panel.add(lcl_maxImageTexUnits, "wrap");
				
				GL11.glGetInteger(ARBVertexShader.GL_MAX_VARYING_FLOATS_ARB, mcl_buf);
				JLabel		lcl_maxVaryingFloats = makeALabel("GL_MAX_VARYING_FLOATS: " + mcl_buf.get(0));
				lcl_panel.add(lcl_maxVaryingFloats, "wrap");
				
				JLabel		lcl_shaderLangVers = makeALabel("Shader Language version " + GL11.glGetString(ARBShadingLanguage100.GL_SHADING_LANGUAGE_VERSION_ARB));
				lcl_panel.add(lcl_shaderLangVers, "wrap");
				
				JLabel		lcl_textureUnitsSupported = makeALabel("Texture Units Supported By Card " + TextureState.getNumberOfTotalUnits());
				lcl_panel.add(lcl_textureUnitsSupported, "wrap");
				
				lcl_thisPanel.add(lcl_panel, "wrap");
				return null;
			}
		});
	}
	
	private	void	filter() {
		String		lstr_filter = mcl_input.getText();
		
		DefaultListModel		lcl_model = new DefaultListModel();
		
		for(String lstr_ext : mstra_extensions) {
			if(lstr_ext.indexOf(lstr_filter) != -1) {
				lcl_model.addElement(lstr_ext);
			}
		}
		
		mcl_extList.setModel(lcl_model);
	}

	public void apply() {
	}
	
	
}
